package com.li.day.day17;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.sql.*;

public class Strre extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Connection co = null;
        PreparedStatement pp = null;
        ResultSet rr = null;
        req.setCharacterEncoding("gbk");
        String name = new String(req.getParameter("name").getBytes("iso-8859-1"), "utf-8");
        String mima = new String(req.getParameter("mima").getBytes("iso-8859-1"), "utf-8");
        String s = "select * from ppro where user_name='" + name + "'AND user_password='" + mima + "'";
        System.out.println(s);
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            co = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/p3kao", "root", "root");
            pp = co.prepareStatement(s);
            rr = pp.executeQuery();
            if (rr.next()) {
                HttpSession ss = req.getSession();
                ss.setAttribute("name3",name);
                req.getRequestDispatcher("Huan").forward(req, resp);
            } else {
                req.getRequestDispatcher("Denglu").forward(req,resp);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            if (rr != null) {
                try {
                    rr.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (pp != null) {
                try {
                    pp.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (co != null) {
                try {
                    co.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}
